package cn.com.oceansoft.business.szjf.controller;

import cn.com.oceansoft.business.base.controller.BaseController;
import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.szjf.entity.Expert;
import cn.com.oceansoft.business.szjf.entity.ExpertList;
import cn.com.oceansoft.business.szjf.entity.ExpertsTemp;
import cn.com.oceansoft.business.szjf.service.IExpertService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

/**
 * 抽取专家
 * Created by Administrator on 2017/7/4.
 */
@Controller
@RequestMapping("expert")
public class ExpertController extends BaseController {
    @Resource
    private IExpertService expertService;


    /**
     * 获取当前日期的空闲专家
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getFreeExperts")
    public Result getFreeExperts(Expert expert) {
        expert.setExpertstype("A");
        List<Expert> experts_a = expertService.getExperts(expert, "free");//g获取A类空闲专家
        expert.setExpertstype("B");
        List<Expert> experts_b = expertService.getExperts(expert, "free");//g获取A类空闲专家
        Map<String, Integer> map = new HashMap<>();
        map.put("experts_a", experts_a.size());
        map.put("experts_b", experts_b.size());
        return new Result(Result.SUCC, map);
    }

    public int[] getRadom(int number, int size) {
//        int num = 0;
//        try {
//            num = Integer.parseInt(number);
//        } catch (NumberFormatException e) {
//            e.printStackTrace();
//        }
        int[] no = new int[number];//存放取到的随机数
        int index = 0;
        while (index < number) {
            boolean has = false;//标记取到的随机数是否存在
            //取随机数
            int temp = new Random().nextInt(size);
            //以下代码检测刚取到的随机数是否已经存在
            for (int i = 0; i < index; i++) {
                if (temp == no[i]) {
                    has = true;
                }
            }
            if (!has) {
                no[index] = temp;
                index++;
            }
        }
        return no;
    }
    /**
     * 随机抽取专家
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "sjcqzj")
    public Result sjcqzj(String tdate, int num_a, int num_b,String paguid) {
        Expert expert = new Expert();
        expert.setTdate(tdate);
        expert.setExpertstype("A");
        List<Expert> experts_a = expertService.getExperts(expert, "free");//g获取A类空闲专家
        int[] ram_a = getRadom(num_a, experts_a.size());
        List<Expert> result = new ArrayList<Expert>();
        for (int i = 0; i < num_a; i++) {
            result.add(experts_a.get(ram_a[i]));
        }
        expert.setExpertstype("B");
        List<Expert> experts_b = expertService.getExperts(expert, "free");//g获取A类空闲专家
        int[] ram_b = getRadom(num_b, experts_b.size());
        for (int i = 0; i < num_b; i++) {
            result.add(experts_b.get(ram_b[i]));
        }
        //将结果保存在临时专家表中
        ExpertsTemp expertsTemp=new ExpertsTemp();
        for(int i=0;i<result.size();i++){
            expertsTemp.setUserguid(result.get(i).getGuid());
            expertsTemp.setFkey(paguid);
            expertsTemp.setIsleave("0");
            expertsTemp.setRemark("1");
            expertService.savezj(expertsTemp);
        }



        return new Result(Result.SUCC, result);
    }

    /**
     * 重新抽取专家
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "cxcqzj")
    public Result cxcqzj(String tdate, String type,String paguid) {
        Expert expert = new Expert();
        expert.setTdate(tdate);
        expert.setExpertstype(type);
        expert.setPaguid(paguid);
        List<Expert> experts = expertService.getExpertsPart(expert, "free");//g获取A类 或B类空闲专家
        if(experts.size()<1){
            return new Result(Result.FAILURE, "剩余专家不足！");
        }
        int[] ram = getRadom(1, experts.size());
        List<Expert> result = new ArrayList<Expert>();
        result.add(experts.get(ram[0]));

        ExpertsTemp expertsTemp=new ExpertsTemp();
        expertsTemp.setUserguid(result.get(0).getGuid());
        expertsTemp.setFkey(paguid);
        expertsTemp.setIsleave("0");
        expertsTemp.setRemark("1");
        expertService.savezj(expertsTemp);
        return new Result(Result.SUCC, result);
    }

    @ResponseBody
    @RequestMapping("getBack")
    public Result goBack(String paguid){
        //删除临时表数据expertstemp
        expertService.deleteExpertsTemp(paguid);
        return new Result(Result.SUCC,null);
    }
    /**
     * 保存专家
     * @param expertList
     * @param flag 标示来源 评审或者验收
     * @param paguid 标示guid 评审或者验收
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "savezj")
    public Result savezj(ExpertList expertList,String flag,String paguid,String tdate,String time){

        try {
            if(expertList != null && expertList.getExpertList().size()!=0){
                List<Expert> experts = expertList.getExpertList();

                expertService.cqzj(experts,flag,paguid,tdate,time,getSession());

            }else{
                return new Result(Result.FAILURE,"尚未随机抽取专家");
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Result(Result.FAILURE,"保存专家失败");
        }

        return new Result(Result.SUCC,"保存专家成功");
    }
}
